<?php

namespace teach\controllers;

use teach\components\WeChat;
use teach\service\Assemble;
use teach\util\helper\ResponseHelper;
use Yii;
use yii\filters\VerbFilter;
use yii\web\Controller;

class AssembleController extends Controller
{
    public $enableCsrfValidation = false;

    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::class,
                'actions' => [
                    '*' => ['post'],
                ],
            ],
        ];
    }

    /**
     * 获取拼团的列表
     * @return \yii\web\Response
     */
    public function actionList()
    {
        $request = Yii::$app->request;
        $mid = $request->post('mid');

        $msg = '';
        $flag = false;
        if (!$mid) {
            $msg = '商家id不能为空';
        } else {
            $flag = true;
        }

        if (!$flag) {
            $jsonData = ResponseHelper::buildJsonData(400, $msg);
        } else {
            $page = $request->post('page');
            $page = $page ? $page : 1;
            $time = time();
            $map = " status = 1 AND mid = {$mid} AND start_time < {$time} AND end_time > {$time}";

            $assembles = new Assemble();
            $assembleList = $assembles->assembleList($map, $page, $pre_page = 10);
            $jsonData = ResponseHelper::buildJsonData(200, '成功', $assembleList);
        }
        return $this->asJson($jsonData);
    }

    /**
     * 拼团产品详情
     * @return \yii\web\Response
     */
    public function actionDetail()
    {
        $request = Yii::$app->request;
        $pid = $request->post('pid');

        $msg = '';
        $flag = false;
        if (!$pid) {
            $msg = '商品id不能为空';
        } else {
            $flag = true;
        }

        if (!$flag) {
            $jsonData = ResponseHelper::buildJsonData(400, $msg);
        } else {
            $assembles = new Assemble();
            $where = " status = 1 AND id = {$pid}";
            $andWhere = " pid = {$pid} ";
            $assemble = $assembles->assembleDetail($where, $andWhere);

            if ($assemble) {
                $jsonData = ResponseHelper::buildJsonData(200, '成功', $assemble);
            } else {
                $jsonData = ResponseHelper::buildJsonData(400, '产品发生错误,联系商家');
            }
        }
        return $this->asJson($jsonData);
    }

    /**
     * 此产品同一组的 团队
     * @return \yii\web\Response
     */
    public function actionGroup()
    {
        $request = Yii::$app->request;
        $pid = $request->post('pid');

        $msg = '';
        $flag = false;
        if (!$pid) {
            $msg = '商品id不能为空';
        } else {
            $flag = true;
        }

        if (!$flag) {
            $jsonData = ResponseHelper::buildJsonData(400, $msg);
        } else {
            $page = $request->post('page');
            $page = $page ? $page : 1;
            $assembles = new Assemble();
            $where = " pid = {$pid}";
            $group = $assembles->assembleGroup($pid ,$where, $page, $pre_page = 10);
            $jsonData = ResponseHelper::buildJsonData(200, '成功', $group);
        }
        return $this->asJson($jsonData);
    }

    /**
     * 申请退款
     * @return \yii\web\Response
     */
    public function actionRefund()
    {
        $request = Yii::$app->request;
        $uid = $request->post('uid');
        $openid = $request->post('openid');
        $mid = $request->post('mid');
        $oid = $request->post('oid');
        $types = $request->post('types');
        $reason = $request->post('reason');
        $amount = $request->post('amount');
        $explain = $request->post('explain');
        $out_trade_no = $request->post('out_trade_no');

        $msg = '';
        $flag = false;

        if (!$uid) {
            $msg = 'uid 用户id不用为空';
        } elseif (!$openid) {
            $msg = 'openid不能为空';
        } elseif (!$mid) {
            $msg = '商家mid不能为空';
        } elseif (!$oid) {
            $msg = '订单id不能为空';
        } elseif (!$types) {
            $msg = '退款类型不能为空';
        } elseif (!$reason) {
            $msg = '申请原因不能为空';
        } elseif (!$amount) {
            $msg = '金额不能为空';
        } elseif (!$explain) {
            $msg = '申请说明不能为空';
        } elseif (!$out_trade_no) {
            $msg = '订单号不能为空';
        } else {
            $flag = true;
        }

        if (!$flag) {
            $jsonData = ResponseHelper::buildJsonData(400, $msg);
        } else {

            $assembles = new Assemble();

            //查找下订单是否存在
            $andWhere = " uid = {$uid} AND out_trade_no = '{$out_trade_no}'";
            $order = $assembles->assembleOrderFind($andWhere);

            if (!$order) {
                $jsonData = ResponseHelper::buildJsonData(400, '订单有误,核实后再申请');
                return $this->asJson($jsonData);
            }

            //查找一下用户是否已经对此单提出了申请
            //$where = " uid = {$uid} AND out_trade_no = '{$out_trade_no}' AND status != -1 ";
            //$result = $assembles->assembleRefundFind($where);

            //if ($result) {
                //$jsonData = ResponseHelper::buildJsonData(400, '订单已经申请过退款,请不要重复提交');
                //return $this->asJson($jsonData);
            //}
            if ($order['status'] == -2) {
                $jsonData = ResponseHelper::buildJsonData(400, '订单已经申请过退款,请不要重复提交');
                return $this->asJson($jsonData);
            }
            if ($order['status'] != 1) {
                $jsonData = ResponseHelper::buildJsonData(400, '此订单不可以申请退款');
                return $this->asJson($jsonData);
            }


            $map = [
                'uid' => $uid,
                'openid'=> $openid,
                'mid' => $mid,
                'oid' => $oid,
                'out_trade_no' => $out_trade_no,
                'types' => $types,
                'reason' => $reason,
                'amount' => $amount,
                'explain' => $explain,
                'img' => $request->post('img'),
                'status' => 1,


            ];
            $res = $assembles->assembleRefund($map,$oid, $out_trade_no);
            if ($res) {
                $jsonData = ResponseHelper::buildJsonData(200, '已成功发送申请,请耐心等待');
            } else {
                $jsonData = ResponseHelper::buildJsonData(400, '申请失败');
            }
        }
        return $this->asJson($jsonData);
    }

    /**
     * // 生成拼团二维码
     */
    public function actionQrcode()
    {
        $request = Yii::$app->request;

        $mid = $request->post('mid');
        $oid = $request->post('oid');
        $uid = $request->post('uid');

        $msg = '';
        $flag = false;

        if (!$oid) {
            $msg = 'oid 订单id不能为空';
        } elseif (!$mid) {
            $msg = 'mid 不能为空';
        } elseif (!$uid) {
            $msg = 'uid 不能为空';
        } else {
            $flag = true;
        }

        if (!$flag) {
            $jsonData = ResponseHelper::buildJsonData(400, $msg);
        } else {
            $WeChat = new WeChat();
            $dir = "teach/web/QRCode/{$mid}/assemble/";
            $scene = "{$oid},{$mid},{$uid}";
            $page = "shopListPage/shoplistpages/detail/detail";
            $QRCode = $WeChat->qrCode($dir, $scene, $page);
            $path = Yii::$app->params['QRCodeUrl'] . $dir . $QRCode;
            $jsonData = ResponseHelper::buildJsonData(200, '成功', $path);
        }
        return $this->asJson($jsonData);
    }

    /**
     * 扫码或者点开小程序拼团
     */
    public function actionLaunch()
    {
        $request = Yii::$app->request;

        $oid = $request->post('oid');
        $uid = $request->post('uid');//进来的用户的id
        $mid = $request->post('mid');

        $msg = '';
        $flag = false;
        if (!$oid) {
            $msg = 'oid 不能为空';
        } elseif (!$uid) {
            $msg = 'uid 不能为空';
        } elseif (!$mid) {
            $msg = 'mid 不能为空';
        } else {
            $flag = true;
        }

        if (!$flag) {
            $jsonData = ResponseHelper::buildJsonData(400, $msg);
        } else {

            $assembles = new Assemble();
            //查看进来的用户是否绑定在此商家下
            $where = "mall_id = {$mid} AND id = {$uid}";
            $user = $assembles->assembleUser($where);
            if (!$user) {
                $jsonData = ResponseHelper::buildJsonData(400, '所在区域不参与此活动');
                return $this->asJson($jsonData);
            }

            //查找订单
            $andWhere = "id = {$oid}";
            $order = $assembles->assembleOrderOne($andWhere);
            if (!$order) {
                $jsonData = ResponseHelper::buildJsonData(400, '订单有误,请检查');
                return $this->asJson($jsonData);
            }

            //根据订单，查找出用户拼团组，产品
            $time = time();
            $whereFilter = " status = 1 AND id = {$order['pid']} AND start_time < {$time} AND end_time > {$time}";
            $andWhereFilter = " pid = {$order['sku_id']} AND status = 1";
            $product = $assembles->assembleProduct($whereFilter, $andWhereFilter);

            if (!$product) {
                $jsonData = ResponseHelper::buildJsonData(400, '拼团活动已经结束');
                return $this->asJson($jsonData);
            }

            //查出组
            $filter = " group_id = '{$order['group_id']}'";
            $group = $assembles->assembleOrderGroup($filter);
            if ($group) {
                $data['product'] = $product;
                $data['group'] = $group;
                $jsonData = ResponseHelper::buildJsonData(200, '成功', $data);
            } else {
                $jsonData = ResponseHelper::buildJsonData(400, '拼团已经结束');
            }

        }
        return $this->asJson($jsonData);
    }

    /**
     * 入口图片
     * @return \yii\web\Response
     */
    public function actionEnter()
    {
        $img_enter_url = 'https://teach-1258261086.cos.ap-guangzhou.myqcloud.com/image/assemble/enter/a93e57893d3ba56cf70b509f555abcf.png';
        $jsonData = ResponseHelper::buildJsonData(200, '成功', $img_enter_url);
        return $this->asJson($jsonData);
    }

}